#include <string>
#include "Player.h"
#include "dv_sdlgrafix.h"

/*--- Constructor/Destructor ---*/
Player::Player() : index(0), scoreClick(0), totalScore(0), pNum(0)
{
}

Player::~Player()
{
	for(int i=0; i < MAX_DICE; i++){
		dice[i].release();
	}
	delete[] dice;
	SDL_FreeSurface(textSurface);
}

/*--- Base Game Functions ---*/
void Player::initialize(int a_index, int a_state, SDL_Color a_color, TTF_Font *a_font)
{
	// run set functions
	setColor(a_color);
	setFont(a_font);
	setIndex(a_index);
	setGameState(a_state);

	pNum = index + 1;

	// create new dice set and initialize them
	dice = new Dice[MAX_DICE];
	for(int i = 0; i < MAX_DICE; i++){
		dice[i].initialize();
		dice[i].setPosition((i*6)+DICE_LOC_X,DICE_LOC_Y);
	}

	score.setFont(font);

	// initalize text surface & rect
	textSurface = TTF_RenderText_Solid(font,PLAYER_TEXT.c_str(),uiTextColor);
	textRect.x = textRect.y = 0;
	textRect.w = textSurface->w;
	textRect.h = textSurface->h;
}

void Player::draw(SDL_Surface *a_buffer)
{
	drawTile(textSurface, &textRect,0,a_buffer,UI_LOC_X,UI_LOC_Y);

	// draw player index on screen
	char * t_index = new char[sizeof(char)];
	_itoa(index+1,t_index,10);
	SDL_Surface * t_Surface = TTF_RenderText_Solid(font, &t_index[0], uiTextColor);
	SDL_Rect t_rect = {0,0,t_Surface->w,t_Surface->h};
	drawTile(t_Surface,&t_rect,0,a_buffer,textSurface->w, UI_LOC_Y);

	// draw all 5 dice on the screen
	for(int i = 0; i < MAX_DICE; i++){
		// if the dice is unrollable draw it blue
		if(dice[i].isFrozen()){
			dice[i].drawF(a_buffer, dice[i].getDiceIndex(), i*50);
		}
		// if the dice is rollable
		else{
			dice[i].draw(a_buffer, dice[i].getDiceIndex(), i*50);
		}
	}

	score.draw(a_buffer);

	// free unused surfaces
	SDL_FreeSurface(t_Surface);
}

void Player::update(SDL_Surface * a_buffer, Position a_position)
{
	switch(pState)
	{
	case PLAYER_IDLE:
		break;
	case PLAYER_ROLL:
		diceRollAll();
		break;
	case PLAYER_SCORE:
		score.update(dice, a_position, a_buffer);
		break;
	}
}

/*--- Main Game Functions ---*/
bool Player::allFrozen()
{
	int t_check = 0;

	// checks for how many dice are frozen
	for(int i = 0; i < MAX_DICE; i++){
		if(dice[i].isFrozen()){ t_check++; }
	}

	// if all dice are return true if not return false
	if(t_check == MAX_DICE){ return true;}
	return false;
}
bool Player::allScored()
{
	int t_score = 0;
	for(int i = 0; i < score.getItems(); i++){
		if(score.isScored(i)) t_score++;
	}
	if(t_score == score.getItems()) return true;
	else return false;
}

void Player::diceRollAll()
{
	for(int i = 0; i < MAX_DICE; i++){
		if(!dice[i].isFrozen()){
			dice[i].diceRoll();
			dice[i].setDiceIndex(dice[i].getDiceValue()-1);
		}
	}
}

void Player::freezeDiceValue(int a_index)
{
	if(!dice[a_index].isFrozen()){
		dice[a_index].setFreeze(true);
	}
	else { dice[a_index].setFreeze(!true); }
}

bool Player::isScoreClicked(Position a_position, int a_w)
{
	if((a_position.y > 0 && a_position.y < (score.getHieght() * score.getItems())) &&
		(a_position.x > (a_w - score.getWidth()) && a_position.x < a_w)){
			return true;
	}
	return false;
}

bool Player::roundScored(int a_gRound)
{
	int t_score = 0;
	for(int i = 0; i < score.getItems(); i++)
		if(score.isScored(i)) t_score++;
	if(t_score > a_gRound) return true;
	else return false;
}

void Player::unFreezeAll()
{
	for(int i = 0; i < MAX_DICE; i++){
		if(dice[i].isFrozen()) dice[i].setFreeze(false);
	}
}